package com.frxuexi.mymvi

import android.util.Log
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import com.frxuexi.mymvi.entity.Record
import kotlinx.coroutines.launch

@Composable
fun NewsPage(vm:NewsViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<Record>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is NewsState.onSuccess->{
                        Log.i("===",it.data.toString())
                        list.addAll(it.data)
                    }
                    is NewsState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }

        launch {
            //发送意图
            vm.channel.send(NewsIntent.getNews(1))
        }
    })
    
    LazyColumn(content = {
        items(list){
            AsyncImage(model = it.imgurl, contentDescription = "")
            Text(text = it.title)
        }
    })
}